package com.tanhua.jym.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.jym.mapper.DaySumMapper;
import com.tanhua.jym.mapper.LogSumMapper;
import com.tanhua.jym.mapper.UserMapper;
import com.tanhua.jym.pojo.AnalysisByDay;
import com.tanhua.jym.pojo.Log;
import com.tanhua.jym.pojo.Retention;
import com.tanhua.jym.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DaySumService111 {
    @Autowired
    private DaySumMapper daySumMapper;

    @Autowired
    private LogSumMapper logSumMapper;
    @Autowired
    private UserMapper userMapper;

    public IPage<AnalysisByDay> findAll(Integer page, Integer pageSize) {
        IPage<AnalysisByDay> infoPage = new Page<>(page, pageSize);
        QueryWrapper<AnalysisByDay> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("record_date");
        this.daySumMapper.selectPage(infoPage, queryWrapper);
        return infoPage;
    }

    public List<Retention> findRetention(String start, String end) {
        long between = DateUtil.between(DateUtil.parseDate(end), DateUtil.parseDate(start), DateUnit.DAY);
        int value = new Long(between).intValue();
        ArrayList<Retention> retentions = new ArrayList<>();
        //获取查询范围
        for (int i = value; i > 0; i--) {
            Retention retention = new Retention();
            DateTime now = DateUtil.offsetDay(DateUtil.parseDate(start), i);
            String format = DateUtil.format(now, "yyyy-MM-dd");
            retention.setDate(DateUtil.format(now, "yyyy-MM-dd"));

            //获取注册数
            QueryWrapper<AnalysisByDay> query = new QueryWrapper<>();
            query.eq("record_date", now);
            AnalysisByDay analysisByDay = this.daySumMapper.selectOne(query);
            //如果当天无记录,跳过
            if (analysisByDay == null) {
                continue;
            }
            //如果当天无注册,返回0
            if (analysisByDay.getNumRegistered() == 0) {
                retention.setDay01("0");
                retention.setDay02("0");
                retention.setDay03("0");
                retention.setDay04("0");
                retention.setDay05("0");
                retention.setDay06("0");
                retention.setDay07("0");
                retention.setDay30("0");
                retention.setSum(0d);
                retentions.add(retention);
                continue;
            }
            int sum = analysisByDay.getNumRegistered();
            Double registerNum = Double.valueOf(sum);
            retention.setSum(registerNum);
            QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("log_time", format).eq("type", "01");
            //获取每天的登录人id
            List<Log> list = this.logSumMapper.selectList(queryWrapper);
            if (CollUtil.isNotEmpty(list)) {
                QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.in("id", CollUtil.getFieldValues(list, "userId"));
                //获取该天登录人的注册时间
                List<User> users = this.userMapper.selectList(queryWrapper1);

                double day01 = 0;
                double day02 = 0;
                double day03 = 0;
                double day04 = 0;
                double day05 = 0;
                double day06 = 0;
                double day07 = 0;
                double day30 = 0;

                double reg = 0;
                if (CollUtil.isNotEmpty(users)) {
                    for (User user : users) {
                        long between1 = DateUtil.between(user.getCreated(), now, DateUnit.DAY);
                        int days = new Long(between1).intValue();
                        if (days == 0){
                            reg++;
                        }
                        if (days >= 1 ) {
                            day01++;
                        }
                        if (days >= 2 ) {
                            day02++;
                        }
                        if (days >= 3 ) {
                            day03++;
                        }
                        if (days >= 4 ) {
                            day04++;
                        }
                        if (days >= 5 ) {
                            day05++;
                        }
                        if (days >= 6 ) {
                            day06++;
                        }
                        if (days >= 7 ) {
                            day07++;
                        }
                        if (days >= 30) {
                            day30++;
                        }
                    }
                }
                retention.setDay01(String.format("%.2f",((day01-reg) / registerNum)*100)+"%");
                retention.setDay02(String.format("%.2f",((day02-reg) / registerNum)*100)+"%");
                retention.setDay03(String.format("%.2f",((day03-reg) / registerNum)*100)+"%");
                retention.setDay04(String.format("%.2f",((day04-reg) / registerNum)*100)+"%");
                retention.setDay05(String.format("%.2f",((day05-reg) / registerNum)*100)+"%");
                retention.setDay06(String.format("%.2f",((day06-reg) / registerNum)*100)+"%");
                retention.setDay07(String.format("%.2f",((day07-reg) / registerNum)*100)+"%");
                retention.setDay30(String.format("%.2f",((day30-reg) / registerNum)*100)+"%");
                retentions.add(retention);
            }
        }
        return retentions;
    }

}
